package com.ccjq.jq173.shosestore.core.util;



import org.apache.commons.lang3.StringUtils;

import java.util.Random;
import java.util.UUID;

public class UuidUtils
{
    /**
     * 生成唯一标识
     * @return  返回长度为19位纯数字唯一标识
     */
    public static long generateShortUUID()
    {
        long t1=0x7FFFFFFF & System.currentTimeMillis();
        Random random=new Random();
        return t1 << 32 | Math.abs(random.nextInt());
    }

    /**
     * 获取唯一标识
     * @param prefix    前缀
     * @param length    长度（不包含前缀，最大长度是19位；长度越短重复的概率越高）
     * @return  返回指定前缀和长度的唯一标识
     */
    public static String generateShotUUID(String prefix,int length)
    {
        String uuid= UUID.randomUUID().toString().replace("-","");
        String shortUUID=String.valueOf(generateShortUUID());
        char[] uuidChars= uuid.toCharArray();
        char[] shortuuidChars=shortUUID.toCharArray();
        StringBuilder builder=new StringBuilder(StringUtils.isBlank(prefix) ? "" : prefix);
        if(length < 0)
        {
            length =shortUUID.length();
        }
        for(int i=0;i<length;i++)
        {
            int index=Integer.parseInt(String.valueOf(shortuuidChars[i]));
            builder.append(String.valueOf(uuidChars[index]));
        }
        return builder.toString().toUpperCase();
    }

    public static void main(String args[])
    {
        long reslut =generateShortUUID();
        System.out.println("reslut="+reslut);

        String id=generateShotUUID("u",10);
        System.out.println("id="+id);
    }
}
